package com.agricultural.handler.impl;

import com.agricultural.equipment.AgriculturalEquipment;
import com.agricultural.handler.SafetyHandler;
import com.agricultural.state.impl.ErrorState;

// 系统错误处理器
public class SystemErrorHandler implements SafetyHandler {
    private SafetyHandler nextHandler; // 下一个处理器
    
    // 设置下一个处理器
    @Override
    public void setNextHandler(SafetyHandler nextHandler) {
        this.nextHandler = nextHandler;
    }
    
    // 处理安全事件
    @Override
    public boolean handleSafetyEvent(AgriculturalEquipment equipment, String eventType, double parameter) {
        if ("systemError".equals(eventType)) {
            System.out.println("[" + equipment.getType() + "-" + equipment.getId() + "] 系统错误处理器接收到事件");
            
            System.out.println("[" + equipment.getType() + "-" + equipment.getId() + "] 错误: 系统故障，错误代码: " + (int) parameter);
            
            // 停止设备并进入错误状态
            equipment.stop();
            equipment.updateState(new ErrorState(equipment, "系统故障，错误代码: " + (int) parameter));
            
            return true; // 事件已处理
        }
        
        // 如果无法处理，传递给下一个处理器
        if (nextHandler != null) {
            return nextHandler.handleSafetyEvent(equipment, eventType, parameter);
        }
        
        return false; // 没有处理器能够处理此事件
    }
}    